package org.github.core.spring.restful.json;

import java.util.Collection;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.github.core.base.BaseMbpPage;
import static java.util.Collections.emptyList;

/**
 * JSON of page.
 *
 * @param <E> element
 * @author JYD_XL
 * @see java.io.Serializable
 * @see java.util.function.Supplier
 */
@EqualsAndHashCode(callSuper = true)
@AllArgsConstructor
@NoArgsConstructor
@Data
public class JSONPage<E> extends JSONBase implements JSON {
  /** 数据 */
  @NotNull
  private Collection<E> data = emptyList();

  /** 总数 */
  private long total = 0L;

  @Override
  public String toString() {
    return super.toString();
  }

  /**
   * Generator.
   */
  @NotNull
  public static <E> JSONPage<E> of() {
    return new JSONPage<>();
  }

  /**
   * Generator.
   */
  @NotNull
  public static <E> JSONPage<E> of(@NotNull BaseMbpPage<E> page) {
    return new JSONPage<>(page.getRecords(), page.getTotal());
  }

  /**
   * Generator.
   */
  @NotNull
  public static <E> JSONPage<E> of(@NotNull Collection<E> data, long total) {
    return new JSONPage<>(data, total);
  }
}
